package com.example.myapplication.ui.Home.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.myapplication.R;
import com.example.myapplication.app.App;
import com.example.myapplication.model.bean.NewsSummary;
import com.example.myapplication.utils.DisplayUtil;
import com.example.myapplication.utils.ImageLoaderUtils;

import java.util.List;

/**
 * Created by meishiyiyun on 2017/6/7.
 */

public class NewAdapter extends BaseMultiItemQuickAdapter<NewsSummary> {
    public static final int TYPE_ITEM = 0;
    public static final int TYPE_PHOTO_ITEM =1;
    private  List<NewsSummary> data;
    public NewAdapter(List<NewsSummary> data) {
        super(data);
        this.data=mData;
        addItemType(TYPE_ITEM, R.layout.item_news);
        addItemType(TYPE_PHOTO_ITEM,R.layout.item_news_photo);
    }

    @Override
    public int getItemViewType(int position) {
        if (!TextUtils.isEmpty((( List<NewsSummary>)mData).get(position).getDigest())){
            return TYPE_ITEM;
        }
        return  TYPE_PHOTO_ITEM;
    }

    @Override
    protected void convert(BaseViewHolder holder, NewsSummary newsSummary) {
        switch (holder.getItemViewType()) {
            case TYPE_ITEM:
                String title = newsSummary.getLtitle();
                if (title == null) {
                    title = newsSummary.getTitle();
                }
                String ptime = newsSummary.getPtime();
                String digest = newsSummary.getDigest();
                String imgSrc = newsSummary.getImgsrc();

                holder.setText(R.id.news_summary_title_tv,title);
                holder.setText(R.id.news_summary_ptime_tv,ptime);
                holder.setText(R.id.news_summary_digest_tv,digest);
                ImageLoaderUtils.display(mContext,holder.getView(R.id.news_summary_photo_iv),imgSrc);
                holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // NewsDetailActivity.startAction(mContext,holder.getView(R.id.news_summary_photo_iv),newsSummary.getPostid(),newsSummary.getImgsrc());
                    }
                });
                break;
            case TYPE_PHOTO_ITEM:
                String titles = newsSummary.getTitle();
                String ptimes = newsSummary.getPtime();
                holder.setText(R.id.news_summary_title_tv,titles);
                holder.setText(R.id.news_summary_ptime_tv,ptimes);
                setImageView(holder, newsSummary);
                holder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // NewsPhotoDetailActivity.startAction(mContext,getPhotoDetail(newsSummary));
                    }
                });
                break;
        }
        if (!TextUtils.isEmpty(newsSummary.getDigest()))
        {

        }else {

        }
    }
//    /**
//     * 普通样式
//     * @param holder
//     * @param newsSummary
//     * @param position
//     */
//    private void setItemValues(final BaseViewHolder holder, final NewsSummary newsSummary, final int position) {
//        String title = newsSummary.getLtitle();
//        if (title == null) {
//            title = newsSummary.getTitle();
//        }
//        String ptime = newsSummary.getPtime();
//        String digest = newsSummary.getDigest();
//        String imgSrc = newsSummary.getImgsrc();
//
//        holder.setText(R.id.news_summary_title_tv,title);
//        holder.setText(R.id.news_summary_ptime_tv,ptime);
//        holder.setText(R.id.news_summary_digest_tv,digest);
//
//        holder.setImageUrl(R.id.news_summary_photo_iv,imgSrc);
//        holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                // NewsDetailActivity.startAction(mContext,holder.getView(R.id.news_summary_photo_iv),newsSummary.getPostid(),newsSummary.getImgsrc());
//            }
//        });
//    }


//    /**
//     * 图文样式
//     * @param holder
//     * @param position
//     */
//    private void setPhotoItemValues(ViewHolderHelper holder, final NewsSummary newsSummary, int position) {
//        String title = newsSummary.getTitle();
//        String ptime = newsSummary.getPtime();
//        holder.setText(R.id.news_summary_title_tv,title);
//        holder.setText(R.id.news_summary_ptime_tv,ptime);
//        setImageView(holder, newsSummary);
//        holder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                // NewsPhotoDetailActivity.startAction(mContext,getPhotoDetail(newsSummary));
//            }
//        });
//    }
//    private NewsPhotoDetail getPhotoDetail( NewsSummary newsSummary) {
//        NewsPhotoDetail newsPhotoDetail = new NewsPhotoDetail();
//        newsPhotoDetail.setTitle(newsSummary.getTitle());
//        setPictures(newsSummary, newsPhotoDetail);
//        return newsPhotoDetail;
//    }

//    private void setPictures(NewsSummary newsSummary, NewsPhotoDetail newsPhotoDetail) {
//        List<NewsPhotoDetail.Picture> pictureList = new ArrayList<>();
//        if (newsSummary.getAds() != null) {
//            for (NewsSummary.AdsBean entity : newsSummary.getAds()) {
//                setValuesAndAddToList(pictureList, entity.getTitle(), entity.getImgsrc());
//            }
//        } else if (newsSummary.getImgextra() != null) {
//            for (NewsSummary.ImgextraBean entity : newsSummary.getImgextra()) {
//                setValuesAndAddToList(pictureList, null, entity.getImgsrc());
//            }
//        } else {
//            setValuesAndAddToList(pictureList, null, newsSummary.getImgsrc());
//        }
//
//        newsPhotoDetail.setPictures(pictureList);
//    }
//    private void setValuesAndAddToList(List<NewsPhotoDetail.Picture> pictureList, String title, String imgsrc) {
//        NewsPhotoDetail.Picture picture = new NewsPhotoDetail.Picture();
//        if (title != null) {
//            picture.setTitle(title);
//        }
//        picture.setImgSrc(imgsrc);
//
//        pictureList.add(picture);
//    }

    private void setImageView(BaseViewHolder holder, NewsSummary newsSummary) {
        int PhotoThreeHeight = (int) DisplayUtil.dip2px(90);
        int PhotoTwoHeight = (int) DisplayUtil.dip2px(120);
        int PhotoOneHeight = (int)DisplayUtil.dip2px(150);

        String imgSrcLeft = null;
        String imgSrcMiddle = null;
        String imgSrcRight = null;
        LinearLayout news_summary_photo_iv_group=holder.getView(R.id.news_summary_photo_iv_group);
        ViewGroup.LayoutParams layoutParams = news_summary_photo_iv_group.getLayoutParams();

        if (newsSummary.getAds() != null) {
            List<NewsSummary.AdsBean> adsBeanList = newsSummary.getAds();
            int size = adsBeanList.size();
            if (size >= 3) {
                imgSrcLeft = adsBeanList.get(0).getImgsrc();
                imgSrcMiddle = adsBeanList.get(1).getImgsrc();
                imgSrcRight = adsBeanList.get(2).getImgsrc();
                layoutParams.height = PhotoThreeHeight;
                holder.setText(R.id.news_summary_title_tv, App.getInstance()
                        .getString(R.string.photo_collections, adsBeanList.get(0).getTitle()));
            } else if (size >= 2) {
                imgSrcLeft = adsBeanList.get(0).getImgsrc();
                imgSrcMiddle = adsBeanList.get(1).getImgsrc();

                layoutParams.height = PhotoTwoHeight;
            } else if (size >= 1) {
                imgSrcLeft = adsBeanList.get(0).getImgsrc();

                layoutParams.height = PhotoOneHeight;
            }
        } else if (newsSummary.getImgextra() != null) {
            int size = newsSummary.getImgextra().size();
            if (size >= 3) {
                imgSrcLeft = newsSummary.getImgextra().get(0).getImgsrc();
                imgSrcMiddle = newsSummary.getImgextra().get(1).getImgsrc();
                imgSrcRight = newsSummary.getImgextra().get(2).getImgsrc();

                layoutParams.height = PhotoThreeHeight;
            } else if (size >= 2) {
                imgSrcLeft = newsSummary.getImgextra().get(0).getImgsrc();
                imgSrcMiddle = newsSummary.getImgextra().get(1).getImgsrc();

                layoutParams.height = PhotoTwoHeight;
            } else if (size >= 1) {
                imgSrcLeft = newsSummary.getImgextra().get(0).getImgsrc();

                layoutParams.height = PhotoOneHeight;
            }
        } else {
            imgSrcLeft = newsSummary.getImgsrc();

            layoutParams.height = PhotoOneHeight;
        }

        setPhotoImageView(holder, imgSrcLeft, imgSrcMiddle, imgSrcRight);
        news_summary_photo_iv_group.setLayoutParams(layoutParams);
    }

    private void setPhotoImageView(BaseViewHolder holder, String imgSrcLeft, String imgSrcMiddle, String imgSrcRight) {
        if (imgSrcLeft != null) {
            holder.setVisible(R.id.news_summary_photo_iv_left,true);
            ImageLoaderUtils.display(mContext,holder.getView(R.id.news_summary_photo_iv_left),imgSrcLeft);
          //  holder.setImageUrl(R.id.news_summary_photo_iv_left,imgSrcLeft);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_left,false);
        }
        if (imgSrcMiddle != null) {
            holder.setVisible(R.id.news_summary_photo_iv_middle,true);
            ImageLoaderUtils.display(mContext,holder.getView(R.id.news_summary_photo_iv_middle),imgSrcMiddle);
          //  holder.setImageUrl(R.id.news_summary_photo_iv_middle,imgSrcMiddle);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_middle,false);
        }
        if (imgSrcRight != null) {
            holder.setVisible(R.id.news_summary_photo_iv_right,true);
            ImageLoaderUtils.display(mContext,holder.getView(R.id.news_summary_photo_iv_right),imgSrcRight);
            //holder.setImageUrl(R.id.news_summary_photo_iv_right,imgSrcRight);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_right,false);
        }
    }

}
